package com.stars.empoly.vmodel

import android.app.Activity
import android.app.Application
import android.text.TextUtils
import androidx.databinding.Bindable
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.google.gson.Gson
import com.hwangjr.rxbus.RxBus
import com.stars.empoly.BR
import com.stars.empoly.config.ConfigRx
import com.stars.empoly.model.*
import com.stars.empoly.utils.ChosePhotoUtils
import com.stars.empoly.utils.net.NetEngine
import com.tencent.mmkv.MMKV
import com.trailer.base.base.BRepository
import com.trailer.base.base.BViewModel
import kotlinx.coroutines.launch

class AuthDriverVModel(application: Application) : BViewModel<BRepository>(application) {

    // var image: MutableLiveData<String> = MutableLiveData("")

    var type = 1//1:身份证-人像面 2：身份证-国徽面 3：驾驶证-正页 4:驾驶证-副页  5:车辆照片
    var identityZ: MutableLiveData<String> = MutableLiveData("")
    var identityF: MutableLiveData<String> = MutableLiveData("")
    var drivingZ: MutableLiveData<String> = MutableLiveData("")
    var drivingF: MutableLiveData<String> = MutableLiveData("")

    var carImg: MutableLiveData<String> = MutableLiveData("")


    var isOne: MutableLiveData<Boolean> = MutableLiveData(false)
    var isTwo: MutableLiveData<Boolean> = MutableLiveData(false)
    var isThree: MutableLiveData<Boolean> = MutableLiveData(false)
    var isFour: MutableLiveData<Boolean> = MutableLiveData(false)
    var isFive: MutableLiveData<Boolean> = MutableLiveData(false)


    //不完整路径

    var identityZ_up: String? = ""
    var identityF_up: String? = ""
    var drivingZ_up: String? = ""
    var drivingF_up: String? = ""
    var carImg_up: String? = ""


    var attestation=MutableLiveData<String>()
    var isShow: MutableLiveData<Boolean> = MutableLiveData(false)

    var isTips=MutableLiveData<Boolean>(false)//默认不显示

    var listCar: MutableLiveData<List<TypeGet>> = MutableLiveData()//车型
    var type_id: String? = null

    var listCarTo: MutableLiveData<List<TypeGet>> = MutableLiveData()//车型

    var listCarTwo: MutableLiveData<List<TypeGet>> = MutableLiveData()//车型



    @Bindable
    var name: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.name)
        }

    @Bindable
    var card: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.card)
        }

    @Bindable
    var carName: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.carName)
        }

    @Bindable
    var number: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.number)
        }


    fun toSubmit() {

        if (TextUtils.isEmpty(name)) {

            toast("请输入真实姓名")
            return
        }
        if (TextUtils.isEmpty(card)) {
            toast("请输入身份证号")
            return
        }
        if (TextUtils.isEmpty(carName)) {
            toast("请输入车辆名称")
            return
        }
        if (TextUtils.isEmpty(number)) {
            toast("输入车牌号")
            return
        }

        if (TextUtils.isEmpty(carImg.value)) {
            toast("请上传车辆45°角照片")
            return
        }
        if (TextUtils.isEmpty(identityZ.value)) {
            toast("请上传行驶证主页照片")
            return
        }
        if (TextUtils.isEmpty(identityF.value)) {
            toast("请上传行驶证副业照片")
            return
        }
        if (TextUtils.isEmpty(drivingZ.value)) {
            toast("请上传驾驶证正页照片")
            return
        }
        if (TextUtils.isEmpty(drivingF.value)) {
            toast("请上传驾驶证副业图片")
            return
        }
        if(TextUtils.isEmpty(type_id)){
            toast("请选择车型")
            return
        }
        var driving_license = identityZ_up + "," + identityF_up
        var driver_license = drivingZ_up + "," + drivingF_up


        //保存信息
        var  ss= AttestationDetail("","",name,card,carName,carImg.value,type_id,number,"","","",
                identityZ.value,identityF.value,drivingZ.value,drivingF.value
                )

        var  to= AttesDetail(identityZ_up,  identityF_up,  drivingZ_up, drivingF_up,  carImg_up)
        //保存信息
        MMKV.defaultMMKV().putString("item", Gson().toJson(ss))
        MMKV.defaultMMKV().putString("to", Gson().toJson(to))


        viewModelScope.launch {
            var it = repository().lreq { NetEngine.service.attestation(name,type_id, card, carName, number, carImg_up, driving_license, driver_license) }
            if (it.ok(true)) {


                RxBus.get().post(ConfigRx.REFRESH_USER, "")
                //成功的话
                MMKV.defaultMMKV().putString("item", "")
                MMKV.defaultMMKV().putString("to","")

                var con = repository().context as Activity
                con.finish()




            }

        }


    }


    fun choseImg(type: Int) {//1:身份证-人像面 2：身份证-国徽面 3：驾驶证-正页 4:驾驶证-副页  5:车辆照片

        if (isShow.value!!) {
            return
        }
        //选择照片
        this.type = type
        ChosePhotoUtils.getChosePhoto(view()!!.context(), 1)

    }


    fun getInfo(list:List<TypeGet>) {
        viewModelScope.launch {
            var it = repository().preq { NetEngine.service.attestationDetail() }
            if (it.ok()) {
                identityZ.value = it.data?.driving_license_image1
                identityF.value = it.data?.driving_license_image2
                drivingZ.value = it.data?.driver_license_image1
                drivingF.value = it.data?.driver_license_image2
                carImg.value = it.data?.vehicle_avatar

                name = it.data?.owner_name
                card = it.data?.identity_card
                carName = it.data?.vehicle_name
                number = it.data?.plate_number


                type_id=it.data!!.car_type
                for(item in list){
                    if(item.id==type_id){
                        item.chose=true
                    }
                }

                listCar.value=list
                isShow.value = true
            }

        }
    }


    fun getUserInfo(){

        viewModelScope.launch {

            var it  = repository().lreq { NetEngine.service.getUser() }
            if(it.ok(false)){



            }

        }

    }


    fun setInitData(item:AttestationDetail,to:AttesDetail,list:List<TypeGet>){

        identityZ.value =item.driving_license_image1
        identityF.value = item.driving_license_image2
        drivingZ.value = item.driver_license_image1
        drivingF.value = item.driver_license_image2
        carImg.value = item.vehicle_avatar
        name = item.owner_name
        card = item.identity_card
        carName = item.vehicle_name
        number = item.plate_number
        //设置不完整路径
         identityZ_up=to.identityZ_up
         identityF_up=to.identityF_up
         drivingZ_up=to.drivingZ_up
         drivingF_up=to.drivingF_up
         carImg_up=to.carImg_up

         type_id=item.car_type
        for(item in list){
            if(item.id==type_id){
                item.chose=true
            }
        }

    }

    fun getTypeGet(type:Int){//获取车的类型信息
        viewModelScope.launch {
            var it=  repository().lreq { NetEngine.service.typeGet() }
            if(it.ok()){

                if(type==1) {
                    getInfo(it.data!!)
                }else if(type==0){
                    listCarTo.value=it.data
                }else{
                    listCarTwo.value=it.data
                }

            }
        }
    }

}



